A symbol in computer programming is a primitive datatype whose instances have a unique human-readable form. Symbols can be used as identifiers. In some programming languages, they are called atoms.[1]
In the most trivial implementation, they are essentially named integers (e.g. the enumerated type in C).
Contents |
The following programming languages provide support for symbols:
language | type name(s) | example literal(s) |
Clojure | symbol,[2] keyword[3] | symbol, :keyword |
ANSI Common Lisp | symbol, keyword | symbol, :keyword |
R6RS Scheme | symbol | sym |
Ruby | Symbol | :sym or :'sym' |
Smalltalk | Symbol | #sym or #'sym' |
Ericsson Erlang | atom | sym |
A symbol in Lisp is unique in a namespace (called package in Common Lisp). Symbols can be tested for equality with the function EQ. Lisp programs can generate new symbols at runtime. When Lisp reads data that contains textual represented symbols, existing symbols are referenced. If a symbol is unknown, the Lisp reader creates a new symbol.
In Common Lisp symbols have the following attributes: a name, a value, a function, a list of properties and a package.[4]
In Common Lisp symbols may use any characters, including whitespace, such as spaces and newlines. If a symbol contains a whitespace character it needs to be written as |this is a symbol|. Symbols can be used as identifiers for any kind of named programming constructs: variables, functions, macros, classes, types, goto tags and more. Symbols can be interned in a package[5]. Keyword symbols are self-evaluating[6] and interned in the package named KEYWORD.
The following is a simple external representation of Common Lisp symbol:
this-is-a-symbol
Symbols can contain whitespace (and all other characters):
|This is a symbol with whitespace|
In Common Lisp symbols with a leading colon in their printed representations are keyword symbols. These are interned in the keyword package.
:keyword-symbol
A printed representation of a symbol may include a package name. Two colons are written between the name of the package and the name of the symbol.
package-name::symbol-name
Packages can export symbols. Then only one colon is written between the name of the package and the name of the symbol.
package:exported-symbol
In Ruby, symbols can be created with a literal form, or by converting a string[1]. They can be used as an identifier or an interned string[7]. Two symbols with the same contents will always refer to the same object.[8] It is considered a best practice to use symbols as keys to an associative array in Ruby[7][9].
The following is a simple example of a symbol literal in Ruby[1]:
my_symbol = :a my_symbol = :"an identifier"
Strings can be coerced into symbols, vice versa:
my_symbol = "Hello, world!".intern #=> :"Hello, world!" my_symbol = "Hello, world!".to_sym #=> :"Hello, world!" my_string = :hello.to_s
Symbols are objects of the Symbol
class in Ruby[10]:
my_symbol = :hello_world my_symbol.length #=> 11 my_symbol.class #=> Symbol
Symbols are commonly used to dynamically send messages to (call methods on) objects:
# same as "aoboc".split("o") "aoboc".send(:split, "o") #=> ["a", "b", "c"]
Symbols as keys of an associative array:
my_hash = { a: "apple", b: "banana" } my_hash[:a] #=> "apple" my_hash[:b] #=> "banana"
In Smalltalk, symbols can be created with a literal form, or by converting a string. They can be used as an identifier or an interned string. Two symbols with the same contents will always refer to the same object.[11] In most Smalltalk implementations, selectors (method names) are implemented as symbols.
The following is a simple example of a symbol literal in Smalltalk:
my_symbol := #'an identifier' " Symbol literal " my_symbol := #a " Technically, this is a selector literal. In most implementations, " " selectors are symbols, so this is also a symbol literal "
Strings can be coerced into symbols, vice versa:
my_symbol := 'Hello, world!' asSymbol " => #'Hello, world!' " my_string := #hello: asString " => 'hello:' "
Symbols conform to the symbol
protocol, and their class is called Symbol
in most implementations:
my_symbol := #hello_world my_symbol class " => Symbol "
Symbols are commonly used to dynamically send messages to (call methods on) objects:
" same as 'foo' at: 2 " 'foo' perform: #at: with: 2 " => $o "